package com.cbs.android.component.fileutils;

import com.cbs.android.component.log.L;

import java.io.File;
import java.math.BigDecimal;

/**
 * Created by broche on 3/31/14.
 */
public final class FileUtils {
    private static final String TAG = FileUtils.class.getName();

    /**
     * 获取文件夹大小
     *
     * @param file File实例
     * @return long
     */
    public static long getFolderSize(File file) {
        long size = 0;
        try {
            File[] fileList = file.listFiles();
            if (fileList != null) {
                for (File aFileList : fileList) {
                    if (aFileList.isDirectory()) {
                        size = size + getFolderSize(aFileList);
                    } else {
                        size = size + aFileList.length();

                    }
                }
            }
        } catch (Exception e) {
            L.e(TAG, "", e);
        }
        return size;
    }

    public static String getFormattedFolderSize(File file) {
        return getFormattedSize(getFolderSize(file));
    }

    /**
     * 删除指定目录下文件及目录
     *
     * @param file
     * @param deleteThisPath
     * @return
     */
    public static void deleteFolder(File file, boolean deleteThisPath) {
        try {
            if (file.isDirectory()) {  // 处理目录
                File files[] = file.listFiles();
                for (File tfile : files) {
                    deleteFolder(tfile, true);
                }
            }
            if (deleteThisPath) {
                if (!file.isDirectory()) {  // 如果是文件，删除
                    file.delete();
                } else {  // 目录
                    if (file.listFiles().length == 0) {// 目录下没有文件或者目录，删除
                        file.delete();
                    }
                }
            }
        } catch (Exception e) {
            L.w(TAG, "", e);
        }
    }

    /**
     * 格式化单位
     *
     * @param size
     * @return
     */
    public static String getFormattedSize(long size) {
        double kiloByte = size * 1.0 / 1024;
        if (kiloByte < 1) {
            return size + "B";
        }

        double megaByte = kiloByte / 1024;
        if (megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
        }

        double gigaByte = megaByte / 1024;
        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
        }

        double teraBytes = gigaByte / 1024;
        if (teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
        }

        BigDecimal result4 = new BigDecimal(teraBytes);
        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
    }
}
